package com.tro.common.util.office;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;

import com.tro.common.util.string.StringUtlis;

public class pptToHtml {
	public static String pptToHtml(String fileName, String path, String imgPath) {

		String htmlStr = "转换失败";

		try {
			HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(path + fileName));
			Dimension pgsize = ppt.getPageSize();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < ppt.getSlides().size(); i++) {
				for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
					if (shape instanceof HSLFTextShape) {
						HSLFTextShape tsh = (HSLFTextShape) shape;
						for (HSLFTextParagraph p : tsh) {
							for (HSLFTextRun r : p) {
								r.setFontFamily("����");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				// render
				ppt.getSlides().get(i).draw(graphics);

				String imagePath = imgPath + StringUtlis.getGUID() + ".png";
				sb.append("<br>");
				sb.append("<img src=" + "\"" + imagePath + "\"" + "/>");
				FileOutputStream out = new FileOutputStream(imagePath);
				javax.imageio.ImageIO.write(img, "png", out);
				out.close();

			}
			System.out.println("success");
			htmlStr = sb.toString();
		} catch (FileNotFoundException e) {
			System.out.println(e);
			// System.out.println("Can't find the image!");
		} catch (IOException e) {
		}
		return htmlStr;
	}
	public static void main(String[] args) throws IOException {
		
		String content=pptToHtml("spring mvc 配置多视图解析.ppt", "/Users/apple/Desktop/", "/Users/apple/Desktop/image/");
		FileUtils.writeStringToFile(new File("/Users/apple/Desktop/", "人员选择系分.html"), content, "utf-8");
	}
}
